const http = require('http');
const io = require('socket.io');

let httpServer = http.createServer((req,res)=>{

});
httpServer.listen(8080);

let wsServer = io.listen(httpServer);

let sockArr = [];
let i = 0;
wsServer.on('connection', sock=>{
    sockArr.push(sock);
    console.log('有链接过来了');
    //断开链接
    sock.on('disconnect',()=>{
        let n = sockArr.indexOf(sock);
        if(n!=-1){
            sockArr.splice(n,1);
        }
    });

    sock.on('msg', str=>{
        sockArr.forEach((s)=>{
            if(s!=sock){
                s.emit('msg',`${i}:${str}`);
            }
        });
    });
});

setInterval(function(){
    console.log('当前链接数：',sockArr.length);
},500);